#!/usr/bin/env python
"""
    @author: Jean-Lou Dupont
"""
import dbus, gobject, avahi
from dbus import DBusException
from dbus.mainloop.glib import DBusGMainLoop

# Looks for iTunes shares

TYPE = '_daap._tcp'

def service_resolved(*args):
    print 'service resolved'
    print 'name:', args[2]
    print 'address:', args[7]
    print 'port:', args[8]

def print_error(*args):
    print 'error_handler'
    print args[0]
    
def myhandlerRemove(interface, protocol, name, stype, domain, flags):
    print "Removed service: '%s'" % name
    
    
def myhandler(interface, protocol, name, stype, domain, flags):
    print "Found service '%s' type '%s' domain '%s' " % (name, stype, domain)

    if flags & avahi.LOOKUP_RESULT_LOCAL:
            # local service, skip
            pass

    server.ResolveService(interface, protocol, name, stype, 
                          domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), 
                        reply_handler=service_resolved, error_handler=print_error)



loop = DBusGMainLoop()

bus = dbus.SystemBus(mainloop=loop)

server = dbus.Interface( bus.get_object(avahi.DBUS_NAME, '/'), 'org.freedesktop.Avahi.Server')

sbrowser = dbus.Interface(bus.get_object(avahi.DBUS_NAME,
                            server.ServiceBrowserNew(avahi.IF_UNSPEC,
                            avahi.PROTO_UNSPEC, TYPE, 'local', dbus.UInt32(0))),
                            avahi.DBUS_INTERFACE_SERVICE_BROWSER)

sbrowser.connect_to_signal("ItemNew", myhandler)
sbrowser.connect_to_signal("ItemRemove", myhandlerRemove)

gobject.MainLoop().run()
